package com.tools.automatic_clock.service;

import com.tools.automatic_clock.bean.account;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class auto_service {
    @Value("${login_url}")
    String url;
    @Autowired
    private userInfoGetter userinfo;
    int waitlogin =6000;
    public void auto_test() throws Exception{
            List<Integer> snumList = userinfo.numList();
            for (int snum:snumList
                 ) {
                account student = userinfo.selectBySnum(snum);
                String userId = student.getSid();
                String password = student.getSpwd();
                String stuName = student.getName();
                //获取该用户从诞生到现在经过多少天
                int elapsedDate = userinfo.elapsedDate(snum) ;
                //获取用户诞生日期
                int type = userinfo.type(snum);
                if (type-elapsedDate>0){
                    //new ChromeDriver
                    WebDriver driver = seleniumApi.driver(url,"chromedriver","打开浏览器");
                    driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[1]/div[2]/div/input")).sendKeys(userId);
                    driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[2]/div[2]/div/input")).sendKeys(password);
                    driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/button")).click();
                    System.out.println("当前用户账号："+userId);
                    System.out.println("当前用户名："+stuName);
                    System.out.println("已尝试登录...");
                    Thread.sleep(waitlogin);
                    try {
                        WebElement has_or_no = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[2]/div[1]/div[1]/span"));
                        System.out.println("已进入主页...");
                        try {
                            //判断几类用户
                            WebElement one_or_two = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[2]/div[1]/div[5]/span"));
                            System.out.println(stuName+"该用户为一类用户");
                            driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[2]/div[1]/div[2]/img")).click();
                            Thread.sleep(1000);
                            System.out.println("已进入打卡页面...");
                            try {
                                driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[15]/div/div[2]/div/input")).sendKeys("36");
                                //咳嗽
                                driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[17]/div[2]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //乏力
                                driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[17]/div[3]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //胸闷气急
                                driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[17]/div[4]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //腹泻
                                driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[17]/div[5]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //皮疹或疱疹
                                driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[17]/div[6]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //耳下腮部肿胀
                                driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[17]/div[7]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                System.out.println("信息填写完毕...");
                                //立即提交
                                driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[23]/button")).click();
                                //确定
                                driver.findElement(By.xpath("/html/body/div[4]/div[3]/button[2]")).click();
                                Thread.sleep(1000);
                                System.out.println(stuName+"第一次打卡完毕...");
                            }catch (Exception e){
                                System.out.println(stuName+"第一次打卡失败");
                            }
                        }catch (Exception e){
                            System.out.println(stuName+"该用户为二类用户");
                        }
                    }catch (Exception e){
                        System.out.println("该用户"+stuName+"登陆失败");
                    }
                    driver.quit();
                    //第二次打卡
                    WebDriver driver1 = seleniumApi.driver(url,"chromedriver","打开浏览器");
                    driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[1]/div[2]/div/input")).sendKeys(userId);
                    driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[2]/div[2]/div/input")).sendKeys(password);
                    driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/button")).click();
                    System.out.println("当前用户账号："+userId);
                    System.out.println("当前用户名："+stuName);
                    System.out.println("已尝试登录...");
                    Thread.sleep(waitlogin);
                    try {
                        WebElement has_or_no = driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[2]/div[1]/div[1]/span"));
                        System.out.println("已进入主页...");
                        try {
                            WebElement elementtest = null;
                            try {
                                //尝试获取第五个按钮
                                elementtest = driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[2]/div[1]/div[5]/span"));
                                System.out.println(stuName+"该用户为一类用户");
                            }catch (Exception e){
                                System.out.println(stuName+"该用户为二类用户");
                            }
                            if(elementtest != null){//如果有第五个按钮
                                System.out.println("本次一类打卡");
                                System.out.println("已进入主页...");
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[2]/div[1]/div[2]/img")).click();
                                Thread.sleep(1500);
                                System.out.println("已进入打卡页面...");
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[16]/div/div[2]/div/input")).sendKeys("36");
                                //有可变参数
                                //咳嗽
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[18]/div[2]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //乏力
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[18]/div[3]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //胸闷气急
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[18]/div[4]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //腹泻
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[18]/div[5]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //皮疹或疱疹
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[18]/div[6]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //耳下腮部肿胀
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[18]/div[7]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                System.out.println("信息填写完毕...");
                                //立即提交
                                //*[@id="app"]/div/div[24]/button
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[24]/button")).click();
                                //确定
                                //html/body/div[4]/div[3]/button[2]
                                driver1.findElement(By.xpath("/html/body/div[4]/div[3]/button[2]")).click();
                                System.out.println(stuName+"第二次打卡完毕...");
                                Thread.sleep(1000);
                                driver1.quit();
                            }else {
                                System.out.println("本次二类打卡");
                                System.out.println("已进入主页...");
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[2]/div[1]/div[2]/img")).click();
                                Thread.sleep(1500);//*[@id="app"]/div/div[15]/div/div[2]/div/input
                                System.out.println("已进入打卡页面...");
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[14]/div/div[2]/div/input")).click();
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[38]/button[1]")).click();//选择低风险
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[16]/div/div[2]/div/input")).sendKeys("36");
                                //咳嗽
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[18]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //乏力
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[19]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //其他身体异常
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[20]/div/div[2]/div/input")).sendKeys("身体倍棒");
                                //是否就诊
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[22]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //是否有接触过高风险地区人员
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[24]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //是否接触中风险地区人员
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[25]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //是否医学隔离
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[26]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //是否在陕西
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[27]/div[2]/div/div/div/div/div[1]/div/i")).click();
                                //是否居家隔离
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[29]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //是否疑似
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[30]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //是否确诊
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[31]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                //是否有不适症状
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[32]/div[2]/div/div/div/div/div[2]/div/i")).click();
                                System.out.println("信息填写完毕...");
                                //立即提交
                                driver1.findElement(By.xpath("//*[@id=\"app\"]/div/div[37]/button")).click();
                                //确定
                                driver1.findElement(By.xpath("/html/body/div[4]/div[3]/button[2]")).click();
                                System.out.println(stuName+"第一次打卡完毕...");
                                Thread.sleep(1000);
                            }
                        }catch (Exception e){
                            System.out.println("打卡失败");
                        }
                    }catch (Exception e){
                        System.out.println("该用户"+stuName+"登陆失败");
                    }
                    driver1.quit();
                }else {
                    System.out.println("该用户已过期");
                }
            }
    }

}
